// pages/dynamic/dynamic.js
var app = getApp(); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    tabbarIndex: 2,
    tabbar: [
      {
        img: 'https://crm.bxwhl.com/static/tabbar/index.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/index-select.png',
        text: '名片',
        url: '/pages/index/index',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/goods.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/goods-select.png',
        text: '产品',
        url: '/pages/product/product',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/dynamic.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/dynamic-select.png',
        text: '动态',
        url: '/pages/dynamic/dynamic',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/website.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/website-select.png',
        text: '官网',
        url: '/pages/website/website',
      },
       {
        img: 'https://crm.bxwhl.com/static/tabbar/mine.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/mine-select.png',
        text: '我',
        url: '/pages/mine/mine',
      },
    ],
    // 消息条数
    noteNum: 0,
    // 阅读百分比
    readPercent: 0,
    actionSheetHidden: true,
    focusInput: false,
    height: 50,
    isInput: false,
    topbg : '',
  },
  inputFocus(e) {
    this.setData({
      // height: e.detail.height + 50,
      height: 60,
      isInput: true,
    })
  },
  inputBlur() {
    this.setData({
      isInput: false
    })
  },
 
  focusButn: function () {
    this.setData({
      focusInput: true,
      isInput: true
    })
  },

  dynamicInfoShow:function(e){
    var that = this;
    var dynamic = that.data.lists;
    var index =e.currentTarget.dataset.index;
    dynamic[index].showbtn = !dynamic[index].showbtn;
    that.setData({
      lists: dynamic
    })
  },
  // 跳转外部链接页面
  tooutLine:function(e){
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/outLine/outLine?url=' + url,
    })
    
  },
  // 跳转动态文章页
  todynamicLink:function(e){
    var article_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/dynamicLink/dynamicLink?article_id='+article_id,
    })
  },
  previewImage: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var current = e.currentTarget.dataset.src;
    // var urls = that.data.dynamic[index].thumbs;
    wx.previewImage({
      current: current,
      urls: [current],
    })
  },

  // 点击分享
  actionSheetTap: function (e) {
    var dynamicid = e.currentTarget.dataset.id
    var dynamictype = e.currentTarget.dataset.type
    var dynamicindex = e.currentTarget.dataset.index
    var that = this;
    that.setData({
      actionSheetHidden: !that.data.actionSheetHidden,
      dynamicid: dynamicid,
      dynamictype: dynamictype,
      dynamicindex: dynamicindex,
    })
    
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu1: function () {
    this.setData({
      menu: 1,
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu2: function (e) {
    this.setData({
      menu: 2,
      actionSheetHidden: !this.data.actionSheetHidden
    })
    wx.navigateTo({
      url: '/pages/shareCard/shareCard',
    })
  },
  // 滚动计算阅读
  scroll: function (e) {
    var that = this;
    var scrollHeight = e.detail.scrollHeight;
    var scrollTop = e.detail.scrollTop;
    var windowHeight = that.data.windowHeight;
    var height = scrollHeight - windowHeight - 20;
    var scrollPercent = (scrollTop / height) * 100;
    scrollPercent = Math.ceil(scrollPercent);
    var readPercent = that.data.readPercent;
    if (scrollPercent > 100) {
      scrollPercent = 100;
      readPercent = 100;
    } else {
      scrollPercent = scrollPercent;
      if (scrollPercent > readPercent) {
        readPercent = scrollPercent;
      } else {
        readPercent = readPercent;
      }
    }
    that.setData({
      readPercent: readPercent
    })
  },

  zan:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var dynamic = that.data.lists;
    var user = wx.getStorageSync('userInfo')
    app.httpRequest('dynamic/dynamic_reliable', {id: id}, 'get', false, function(res){
      var is_reliable = dynamic[index].is_reliable;
      dynamic[index].showbtn = !dynamic[index].showbtn;
      if(is_reliable == true){
        dynamic[index].is_reliable = false;
        dynamic[index].reliable_num -= 1;
        var i = dynamic[index].reliableList.indexOf(user.nickName);
        dynamic[index].reliableList.splice(i,1);
      }else{
        dynamic[index].is_reliable = true;
        dynamic[index].reliable_num += 1;
        dynamic[index].reliableList.push(user.nickName);
      }
      that.setData({
        lists:dynamic,
      })
      app.userBehavior(is_reliable ? 120 : 121,'动态点赞')
    })
  },
  comment:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var dynamic = that.data.lists;
    dynamic[index].showbtn = !dynamic[index].showbtn;
    that.setData({
      lists: dynamic,
      isInput: true,
      id: id
    })
  },
  subComment:function(e){
    var that = this;
    var content = e.detail.value.content
    var id = that.data.id;
    var lists = that.data.lists;
    that.setData({
      value: '',
    })
    if(!id) return;
    if(!content){
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      })
      return;
    }
    app.httpRequest('dynamic/dynamic_comment', {id: id, content: content}, 'get', false, function(res){
      wx.showToast({
        title: res.msg,
      })
      var newCommnet = [{
        name: wx.getStorageSync('userInfo').nickName,
        comment: content
      }]
      for(var i=0; i < lists.length; i++){
        if(lists[i].dynamic_id == id){
          lists[i].commentList = lists[i].commentList.concat(newCommnet)
        }
      }
      that.setData({
        lists: lists
      })
      app.userBehavior(122,'动态评论')
    })
  },
  // tabbar跳转 
  tabbarNav: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
  onLoad: function (options) {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var startTime = new Date().getTime();
    that.setData({
      startTime: startTime,
      windowHeight: wx.getStorageSync('systemInfo').windowHeight
    })
    that.getDynamicData();
  },
  getDynamicData:function(){
    var that = this;
    app.httpRequest('dynamic/lists', {}, 'get', false, function(res){
      that.setData({
        enterprise: res.data.enterprise,
        lists: res.data.lists.data,
        pageShow: true,
        topbg: res.data.topbg,
        page: ++that.data.page,
      })
    })
  },
  // 获得停留时间，毫秒级
  getStopTime: function () {
    var that = this;
    var endTime = new Date().getTime();
    var startTime = that.data.startTime;
    var stopTime = endTime - startTime;
    that.setData({
      stopTime: stopTime
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(118,'浏览了动态页',stoptime,percent)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(118,'浏览了动态页',stoptime,percent)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    app.httpRequest('dynamic/lists', {page: page}, 'get', false, function(res){
      that.setData({
        enterprise: res.data.enterprise,
        lists: that.data.lists.concat(res.data.lists.data),
        page: ++page,
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    app.userBehavior(119,'分享了动态')
    var dynamictype = that.data.dynamictype;
    var dynamicindex = that.data.dynamicindex;
    if (dynamictype == 1){
      var title = that.data.lists[dynamicindex].content;
      var path = "/pages/dynamicDetails/dynamicDetails?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id')+'&id='+that.data.lists[dynamicindex].dynamic_id;
      var imgurl = that.data.lists[dynamicindex].images[0];
    } else if(dynamictype == 2){
      var title = that.data.lists[dynamicindex].article.article_title;
      var path = "/pages/dynamicLink/dynamicLink?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id')+'&article_id=' + that.data.lists[dynamicindex].article_id;
      var imgurl = that.data.lists[dynamicindex].article.image[0].file_path;
    }else{
      var path = "/pages/dynamic/dynamic?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id');
    }
    return {
      title: title,
      path: path,
      imageUrl: imgurl,
      success: function (a) {
        
      },
      fail: function (a) { }
    };
  }
})